package io.renren.modules.hos.controller;

import java.util.Arrays;
import java.util.Map;

import io.renren.common.utils.ShiroUtils;
import io.renren.modules.hos.entity.DoctorEntity;
import io.renren.modules.hos.entity.HosUserEntity;
import io.renren.modules.hos.entity.PatientEntity;
import io.renren.modules.hos.form.OperationForm;
import io.renren.modules.hos.service.DoctorService;
import io.renren.modules.hos.service.HosUserService;
import io.renren.modules.hos.service.PatientService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.hos.entity.OperationEntity;
import io.renren.modules.hos.service.OperationService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author ryf
 * @email 2559228480@qq.com
 * @date 2021-12-13 11:55:41
 */
@RestController
@RequestMapping("hos/operation")
public class OperationController {
    @Autowired
    private OperationService operationService;
    @Autowired
    private HosUserService hosUserService;
    @Autowired
    private PatientService patientService;
    @Autowired
    private DoctorService doctorService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("hos:operation:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = operationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("hos:operation:info")
    public R info(@PathVariable("id") Long id){
		OperationEntity operation = operationService.getById(id);

        return R.ok().put("operation", operation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("hos:operation:save")
    public R save(@RequestBody OperationEntity operation){
		operationService.save(operation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("hos:operation:update")
    public R update(@RequestBody OperationEntity operation){
		operationService.updateById(operation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("hos:operation:delete")
    public R delete(@RequestBody Long[] ids){
		operationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 病人查看自己做的手术
     */
    @PostMapping("/patientOperation")
    public R selectOperation(@RequestBody Map<String, Object> params){
        HosUserEntity hosUserEntity = hosUserService.getById(ShiroUtils.getHosUserId());
        if (hosUserEntity.getAuthIdentity() != 1){
            return R.error().put("msg", "您无权限");
        }else {
            PatientEntity patientEntity = patientService.selectByUserId(hosUserEntity.getId());
            PageUtils page = operationService.queryByPatientId(patientEntity.getId(), params);
            return R.ok().put("page", page);
        }
    }

    /**
     * 添加手术
     */
    @PostMapping("/addOperation")
    public R addOperation(@RequestBody OperationForm form){
        HosUserEntity user = hosUserService.getById(ShiroUtils.getHosUserId());
        if (user.getAuthIdentity() != 2){
            return R.error().put("msg", "您无权限");
        }else {
            DoctorEntity doctorEntity = doctorService.selectByUserId(user.getId());
            boolean flag = operationService.addOperation(doctorEntity.getDoctorId(), form);
            if (flag){
                return R.ok().put("data", "添加成功");
            }else {
                return R.error().put("data", "添加失败");
            }
        }
    }

    /**
     * 医生查看
     */
    @PostMapping("/doctorOperation")
    public R selectOperationByDoctor(@RequestBody Map<String, Object> params){
        HosUserEntity hosUserEntity = hosUserService.getById(ShiroUtils.getHosUserId());
        if (hosUserEntity.getAuthIdentity() != 2){
            return R.error().put("msg", "您无权限");
        }else {
            DoctorEntity doctorEntity = doctorService.selectByUserId(hosUserEntity.getId());
            PageUtils page = operationService.queryByDoctorId(doctorEntity.getDoctorId(), params);
            return R.ok().put("page", page);
        }
    }

}
